
package service

import (
	"context"
	"log"
	"guli-shopping/product-service/dao"
	"guli-shopping/product-service/protobuff/productProto"
	"strconv"
	"strings"
)

type PmsAttrService struct {
}

func getPmsAttrResponse() *productProto.PmsAttrResponse {
	return &productProto.PmsAttrResponse{}
}

func (this* PmsAttrService) FindAll(ctx context.Context, request *productProto.PmsAttrRequest) (*productProto.PmsAttrResponse, error) {
	response := getPmsAttrResponse()

    if request.Query==nil {
      request.Query= &productProto.PmsAttrQuery{Model: &productProto.PmsAttr{}}
    }
    if request.Query.Page<=0{
        request.Query.Page=1
    }
    if request.Query.Size<=0 {
        request.Query.Size=5
    }

	all, count,err := dao.PmsAttrDao.FindAll(request.Query)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Total=int64(count)
	response.List=all
	response.Code=200
	response.Message="查询成功"
	return response,nil
}

func (this* PmsAttrService) FindById(ctx context.Context, request *productProto.PmsAttrRequest) (*productProto.PmsAttrResponse, error) {
	response := getPmsAttrResponse()
	if request==nil || request.Id==0 {
		response.Code=403
		response.Message="缺少id"
		return response,nil
	}
	all, err := dao.PmsAttrDao.FindById(request.Id)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Model=all
	response.Code=200
	response.Message="查询成功"
	return response,nil
}

func (this* PmsAttrService) Delete(ctx context.Context, request *productProto.PmsAttrRequest) (*productProto.PmsAttrResponse, error) {
	response := getPmsAttrResponse()
	if request==nil || request.Ids==""{
		response.Code=403
		response.Message="需要指定id"
		return response,nil
	}
	//转换id
	ints:=[]int64{}
	split := strings.Split(request.Ids, ",")
	for _, value := range split {
		parseInt, _ := strconv.ParseInt(value, 10, 64)
		ints=append(ints,parseInt)
	}
	err := dao.PmsAttrDao.Delete(ints)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Code=200
	response.Message="删除成功"
	return response,nil
}

func (this* PmsAttrService) Update(ctx context.Context, request *productProto.PmsAttrRequest) (*productProto.PmsAttrResponse, error) {
	response := getPmsAttrResponse()
	if request==nil || request.Model==nil || request.Model.AttrId==0{
		response.Code=403
		response.Message="需要指定对象和id"
		return response,nil
	}
	 err := dao.PmsAttrDao.Update(request.Model)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Code=200
	response.Message="更新成功"
	return response,nil
}

func (this* PmsAttrService) Save(ctx context.Context, request *productProto.PmsAttrRequest) (*productProto.PmsAttrResponse, error) {
	response := getPmsAttrResponse()
	if request==nil || request.Model.AttrId!=0{
		response.Code=403
		response.Message="对象错误"
		return response,nil
	}
	id, err := dao.PmsAttrDao.Save(request.Model)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Id=id
	response.Code=200
	response.Message="保存成功"
	return response,nil
}

func (this* PmsAttrService) FindAttrByCatId(ctx context.Context, request *productProto.PmsAttrRequest) (*productProto.PmsAttrResponse, error) {
	response := getPmsAttrResponse()
	if request==nil || request.CatId==0 {
		response.Code=403
		response.Message="缺少分类ID"
		return response,nil
	}
	all, err := dao.PmsAttrDao.FindAttrByCatId(request.CatId)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.List=all
	response.Code=200
	response.Message="查询成功"
	return response,nil
}











